package uniqid

import (
	"crypto/md5"
	"crypto/rand"
	"encoding/base64"
	"encoding/hex"
	"strings"
)

// Generate 利用随机数生成Guid字串
func Generate(upper bool, half bool) string {
	b := make([]byte, 48)
	rand.Read(b)
	h := md5.New()
	h.Write([]byte(base64.URLEncoding.EncodeToString(b)))
	result := hex.EncodeToString(h.Sum(nil))
	result = strings.ToUpper(result)
	if upper == true {
		result = strings.ToUpper(result)
	}
	if half == true {
		result = result[8:24]
	}
	return result
}